package com.oocl.parser.impl;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

import com.oocl.communicator.Sender;
import com.oocl.constant.Constant;
import com.oocl.constant.LogLevel;
import com.oocl.constant.ProtocolType;
import com.oocl.operation.OnlineUserDao;
import com.oocl.operation.UserThreadOperation;
import com.oocl.operation.impl.OnlineUserDaoImpl;
import com.oocl.operation.impl.UserTheadOpetationImpl;
import com.oocl.parser.ProtocolParser;
import com.oocl.pojo.Protocol;
import com.oocl.server.UserThread;
import com.oocl.util.ConsoleLogUtil;

public class ProtocolParseImpl implements ProtocolParser {
	private Sender sender;
	private OnlineUserDao userDao;
	private UserThreadOperation threadOperation;
	
	public ProtocolParseImpl(Sender sender) {
		this.sender = sender;
		userDao = new OnlineUserDaoImpl();
		threadOperation = new UserTheadOpetationImpl();
	}

	@Override
	public void parsePrivateTalk(Protocol protocol) throws IOException {
		UserThread thread = threadOperation.findByName(protocol.getTo());
		if(thread==null){
			return;
		}
		thread.getSender().send(protocol);
	}

	@Override
	public void parseGroupTalk(Protocol protocol) throws IOException {
		Map<String, UserThread> threads = threadOperation.findAll();
		if(null==threads || threads.size()==0){
			return;
		}
		for(String name:threads.keySet()){
			if(name.equals(protocol.getFrom())){
				continue;
			}
			threads.get(name).getSender().send(protocol);
		}
	}

	@Override
	public void parsePrivateShake(Protocol protocol) throws IOException {
		UserThread thread = threadOperation.findByName(protocol.getTo());
		if(thread==null){
			return;
		}
		thread.getSender().send(protocol);
	}

	@Override
	public void parseGroupShake(Protocol protocol) throws IOException {
		Map<String, UserThread> threads = threadOperation.findAll();
		if(null==threads || threads.size()==0){
			return;
		}
		for(String name:threads.keySet()){
			if(name.equals(protocol.getFrom())){
				continue;
			}
			threads.get(name).getSender().send(protocol);
		}
	}

	@Override
	public void parseLoyout(Protocol protocol,UserThread userThread) throws IOException {
		ConsoleLogUtil.log("User "+protocol.getFrom()+" layout sucess", LogLevel.INFO);
		Map<String, UserThread> threads = threadOperation.findAll();
		threadOperation.removeByName(protocol.getFrom());
		userDao.delete(protocol.getFrom());
		Protocol pro1 = new Protocol(ProtocolType.USER_LAYOUT, protocol.getFrom(), protocol.getTo(),Constant.USERLAYOUT_NOTICE, new Date());
		for(String s:threads.keySet()){
			threads.get(s).getSender().send(pro1);
		}
		protocol.setType(ProtocolType.LAYOUT);
		sender.send(protocol);
		userThread.setRunning(false);
	}

	@Override
	public void parseLoadUsers(Protocol protocol, UserThread userThread) throws IOException {
		String name = protocol.getFrom();
		ConsoleLogUtil.log("User "+name+" loginning..", LogLevel.INFO);
		
		userDao.add(name);
		protocol.setContent("login success!");
		protocol.setOnlineUsers(userDao.findAll());
		//向所有用户发送用户登陆成功的信息
		Protocol pro1 = new Protocol(ProtocolType.USER_LOGIN, protocol.getFrom(), protocol.getTo(),Constant.USERLOGIN_NOTICE, new Date());
		Map<String, UserThread> threads = threadOperation.findAll();
		for(String s:threads.keySet()){
			threads.get(s).getSender().send(pro1);
		}
		threadOperation.add(name, userThread);
		sender.send(protocol);
		ConsoleLogUtil.log("User "+name+" logined sucess", LogLevel.INFO);
		
	}

}
